<?php

/**
 * This is the model class for table "zy_full_routing".
 *
 * The followings are the available columns in table 'zy_full_routing':
 * @property integer $full_routing_id
 * @property integer $vpnlist_id
 * @property string $open_time
 * @property integer $mark
 * @property integer $user_id
 * @property string $create_time
 * @property string $update_time
 */
class FullRouting extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return FullRouting the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_full_routing';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('vpnlist_id, mark, user_id', 'numerical', 'integerOnly'=>true),
			array('open_time, create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('full_routing_id, vpnlist_id, open_time, mark, user_id, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'full_routing_id' => 'Full Routing',
			'vpnlist_id' => 'Vpnlist',
			'open_time' => 'Open Time',
			'mark' => 'Mark',
			'user_id' => 'User',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('full_routing_id',$this->full_routing_id);
		$criteria->compare('vpnlist_id',$this->vpnlist_id);
		$criteria->compare('open_time',$this->open_time,true);
		$criteria->compare('mark',$this->mark);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	/**
	 * 跟mark的值判断返回相应内容
	 * @param unknown $mark
	 * @return Ambigous <string, string, unknown>
	 */
	public function getMarkCnName($mark){
		if($mark == 0){
			$cnMark  = Yii::t('base','Open Mark');
		}else{
			$cnMark  = Yii::t('base','Close Mark');
		}
		return $cnMark;
	}
	/**
	 * 更改全路由的开启或关闭状态
	 * @param unknown $id
	 * @param unknown $mark
	 */
	public function changeMark($id,$mark){
		if($id !== '' && $mark !=''){
			if($mark == 0){
				$mark = 1;
			}else{
				$mark = 0;
			}
			$sql="update zy_full_routing set `open_time`='0000-00-00 00:00:00' , `update_time`=now() where `full_routing_id`=$id";
			$userservice_result=yii::app()->db->createCommand($sql);
			$userservice_result->execute();
		}
	}
	
	/**
	 * 根据mark值得不同进行切换不同按钮
	 * @param unknown $mark
	 * @return string
	 
	public function  conversionButtons($mark){
		var_dump($mark);		
		if($mark === 1){
			$button = '{delete}{update}{Open}';
		}else{
			$button = '{delete}{update}{Close}';
		}
		return $button;
	}
	*/
	
	/**
	 * 根据用户ID找出用户所购买的服务
	 * 'url'=>'Yii::app()->createUrl("fullRouting/ChooseServer",array("id"=>$data->full_routing_id,"user_id"=>$data->user_id))',
	 * @param unknown $userId
	 * @return unknown
	 */
	public function chooseServer($userId){
		if($userId != ''){
			$sql='SELECT * FROM zy_vpnlist WHERE package_id IN (select package_id from zy_user_service where user_id='.$userId.')';
			$result = Yii::app()->db->createCommand($sql)->queryAll();
		}
		return CHtml::listData($result,'id','comment');
	}
	
}